package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.manager.CustomizedExporeManager;
import com.settlement.system.model.entity.common.CustomizedExporeParams;
import com.settlement.system.model.entity.common.TblCustomizedExporeDtl;
import com.settlement.system.model.entity.common.TblCustomizedExporeMain;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblCustomizedExporeMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自定义导出主表 前端控制器
 *
 * @author Ximi
 * @since 2020-11-17
 */

@Api(tags = "自定义导出主表", description = "自定义导出主表")
@RestController
@RequestMapping("/customized/expore")
public class TblCustomizedExporeMainController extends BaseController<TblCustomizedExporeMain> {

    @Resource
    private ITblCustomizedExporeMainService tblCustomizedExporeMainService;

    @Resource
    private CustomizedExporeManager customizedExporeManager;

    @Override
    public IBaseService init() {
        return tblCustomizedExporeMainService;
    }

    @ApiOperation("获取所有的自定义菜单")
    @RequestMapping(value = "/allMenu", method = {RequestMethod.GET})
    @ResponseBody
    public ObjectDataResponse getAllCustomizedmenu() {
        try {
            List<String> list = customizedExporeManager.getAllMenu();
            return new ObjectDataResponse(list, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @ApiOperation("获取菜单的字段")
    @RequestMapping(value = "/menuFields", method = {RequestMethod.GET})
    @ResponseBody
    public ObjectDataResponse getCustomizedField(@RequestParam("menuName") String menuName) {
        try {
            List<CustomizedExporeManager.CustomizedField> fields =
                    customizedExporeManager.getCustomizedField(menuName);
            return new ObjectDataResponse(fields, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @ApiOperation("获取自定义导出的字段")
    @RequestMapping(value = "/customizedExporeFields", method = {RequestMethod.GET})
    @ResponseBody
    public ObjectDataResponse getCustomizedExporeFields(@RequestParam("menuName") String menuName,
                                                        @RequestParam("customizedExporeName") String customizedExporeName) {
        try {
            List<TblCustomizedExporeDtl> list = tblCustomizedExporeMainService.getByBothName(menuName, customizedExporeName);
            return new ObjectDataResponse(list, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @ApiOperation("获取自定义导出的字段")
    @RequestMapping(value = "/menuAndCustomizedExporeFields", method = {RequestMethod.GET})
    @ResponseBody
    public ObjectDataResponse getCustomizedField(@RequestParam("menuName") String menuName,
                                                 @RequestParam("customizedExporeName") String customizedExporeName) {
        try {
            HashMap<String, Object> map = new HashMap<>(16);
            List<TblCustomizedExporeDtl> customizedExporeFields = tblCustomizedExporeMainService.getByBothName(menuName, customizedExporeName);
            List<CustomizedExporeManager.CustomizedField> menuFields = customizedExporeManager.getCustomizedField(menuName);
            map.put("customizedExporeFields", customizedExporeFields);
            map.put("menuFields", menuFields);
            map.put("menuName", menuName);
            map.put("customizedExporeName", customizedExporeName);
            return new ObjectDataResponse(map, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @ApiOperation("保存自定义导出")
    @RequestMapping(value = "/saveCustomizedExpore", method = {RequestMethod.POST})
    @ResponseBody
    public ObjectDataResponse saveCustomizedExpore(@RequestBody CustomizedExporeParams params) {
        try {

            if (CollectionUtils.isEmpty(params.getFields())) {
                return new ObjectDataResponse("导出字段不能为空", ReturnStatusEnum.CHECK_FAILURE);
            }

            tblCustomizedExporeMainService.saveCustomizedExpore(params);
            return new ObjectDataResponse(null, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(), ex.getMessage());
        }
    }

    @ApiOperation("更新自定义导出")
    @RequestMapping(value = "/updateCustomizedExpore", method = {RequestMethod.POST})
    @ResponseBody
    public ObjectDataResponse updateCustomizedExpore(@RequestBody CustomizedExporeParams params) {
        try {

            if (StringUtils.isEmpty(params.getMainId())) {
                return new ObjectDataResponse("没有需要修改的数据", ReturnStatusEnum.CHECK_FAILURE);
            }

            if (CollectionUtils.isEmpty(params.getFields())) {
                return new ObjectDataResponse("导出字段不能为空", ReturnStatusEnum.CHECK_FAILURE);
            }
            tblCustomizedExporeMainService.updateCustomizedExpore(params);
            return new ObjectDataResponse(null, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(), ex.getMessage());
        }
    }

    @ApiOperation("删除自定义导出")
    @RequestMapping(value = "/deleteCustomizedExpore", method = {RequestMethod.POST})
    @ResponseBody
    public ObjectDataResponse deleteCustomizedExpore(@RequestBody CustomizedExporeParams params) {
        try {
            if (StringUtils.isEmpty(params.getMainId())) {
                return new ObjectDataResponse("没有需要删除的数据", ReturnStatusEnum.CHECK_FAILURE);
            }
            tblCustomizedExporeMainService.deleteCustomizedExpore(params);
            return new ObjectDataResponse(null, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(), ex.getMessage());
        }
    }


    @ApiOperation("获取自定义导出的名称")
    @RequestMapping(value = "/customizedExporeNames", method = {RequestMethod.GET})
    @ResponseBody
    public ObjectDataResponse getCustomizedExporeNames(@RequestParam("menuName") String menuName) {
        try {
            List<String> list = tblCustomizedExporeMainService.getCustomizedExporeNameByMenuName(menuName);
            if (CollectionUtils.isEmpty(list)) {
                list = new ArrayList<>();
            }
            list.add(menuName + "(默认)");
            return new ObjectDataResponse(list, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

}

